{template 'header'}
<div class="page home" id="page-app-index">
	<span id="js-lat" class="hide">{$_GPC['lat']}</span>
	<span id="js-lng" class="hide">{$_GPC['lng']}</span>
	<header class="bar bar-nav">
		<a class="pull-right search-block" href="{php echo $this->createMobileUrl('hunt');}">
			<i class="fa fa-search"></i>
		</a>
		<h1 class="title">
			<a id="position" class="external" href="{php echo $this->createMobileUrl('location');}">{$_GPC['address']}</a>  <i class="fa fa-arrow-down-fill"></i>
		</h1>
	</header>
	{template 'nav'}
	<div class="content">
		{if !empty($slides)}
			<div class="swiper-container slide" data-space-between='0' data-pagination='.swiper-pagination' data-autoplay="2000">
				<div class="swiper-wrapper">
					{loop $slides $slide}
						<div class="swiper-slide" data-link="{$slide['link']}">
							<img src="{php echo tomedia($slide['thumb']);}"alt="">
						</div>
					{/loop}
				</div>
				<div class="swiper-pagination"></div>
			</div>
		{/if}

		<div class="swiper-container category" data-space-between='0' data-pagination='.swiper-category-pagination' data-autoplay="0">
			<div class="swiper-wrapper">
				{loop $categorys_chunk $row}
					<div class="swiper-slide" data-link="{$slide['link']}">
						<div class="row no-gutter nav">
							{loop $row $category}
								<div class="col-25">
									<a href="{php echo $this->createMobileUrl('search', array('cid' => $category['id']));}">
										<img src="{php echo tomedia($category['thumb']);}" alt="{$category['title']}" />
										<div class="text-center">{$category['title']}</div>
									</a>
								</div>
							{/loop}
						</div>
					</div>
				{/loop}
			</div>
			{if count($categorys_chunk) > 1}
				<div class="swiper-pagination swiper-category-pagination"></div>
			{/if}
		</div>

		{if $_W['we7_wmall']['config']['imgnav_status'] == 1 && !empty($_W['we7_wmall']['config']['imgnav_data'])}
			<div class="row no-gutter sborder activity">
				{loop $_W['we7_wmall']['config']['imgnav_data'] $i $nav}
					<div class="col-50 sborder">
						<a href="{$nav['link']}">
							<div class="row no-gutter">
								{if $i % 2 == 0}
									<div class="col-60">
										<div class="heading">{$nav['title']}</div>
										<div class="sub-heading">{$nav['tips']}</div>
									</div>
									<div class="col-40 text-center">
										<img src="{php echo tomedia($nav['img']);}" alt="" />
									</div>
								{else}
									<div class="col-40 text-center">
										<img src="{php echo tomedia($nav['img']);}" alt="" />
									</div>
									<div class="col-60">
										<div class="heading">{$nav['title']}</div>
										<div class="sub-heading">{$nav['tips']}</div>
									</div>
								{/if}
							</div>
						</a>
					</div>
					{php $i++}
				{/loop}
			</div>
		{/if}
		<div class="buttons-tab select-tab">
			<a href="javascript:;" class="button">商家分类 <span class="fa"></span></a>
			<div class="drop-menu-list">
				<div class="list-block">
					<ul>
						<li><a class="list-button item-link" href="{php echo $this->createMobileUrl('search', array('cid' => 0));}">全部</a></li>
						{loop $categorys $row}
							<li><a class="list-button item-link" href="{php echo $this->createMobileUrl('search', array('cid' => $row['id']));}">{$row['title']}</a></li>
						{/loop}
					</ul>
				</div>
			</div>
			<a href="javascript:;" class="button">智能排序 <span class="fa"></span></a>
			<div id="ceshidiv"></div>
			<div class="drop-menu-list">
				<div class="list-block">
					<ul>
						<li><a class="list-button item-link" href="{php echo $this->createMobileUrl('search', array('order' => ''));}"><span class="icon"></span>全部</a></li>
						{loop $orderbys $row}
						<li><a class="list-button item-link"  href="{php echo $this->createMobileUrl('search', array('order' => $row['key']));}">{$row['title']}</a></li>
						{/loop}
					</ul>
				</div>
			</div>
			<a href="javascript:;" class="button">优惠活动 <span class="fa"></span></a>
			<div class="drop-menu-list">
				<div class="list-block">
					<ul>
						<li><a class="list-button item-link" href="{php echo $this->createMobileUrl('search', array('dis' => ''));}"><span class="icon"></span>全部</a></li>
						{loop $discounts $row}
							<li><a class="list-button item-link" href="{php echo $this->createMobileUrl('search', array('dis' => $row['key']));}"><span class="{$row['css']}"></span>{$row['title']}</a></li>
						{/loop}
					</ul>
				</div>
			</div>
		</div>
		<div class="store-list store-empty" id="store-list">
			<div class="common-no-con">
				<img src= "{MODULE_URL}resource/app/img/store_no_con.png" alt="" />
				<p>抱歉,没有符合条件的商户！</p>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=4c1bb2055e24296bbaef36574877b4e2"></script>
<script type="text/javascript" src="../addons/we7_wmall/resource/app/js/jquery.quicksand.js"></script>
<?php $sql1="SELECT * FROM ". tablename("goto_history"). " WHERE openid='".$_SESSION['openid']."'";
	 $mission=pdo_fetch($sql1);
	 if(!empty($mission['shopid'])){
	 ?>

	
		<script language="javascript" type="text/javascript"> 
		if(sessionStorage.getItem("is_first")!=1){
			 
		   window.location.href="index.php?i=65&c=entry&sid=<?php echo $mission['shopid']?>&do=goods&m=we7_wmall"; 
		}
		</script>
		<?php }?>

<script>
$(function(){
	$(document).on('click', '.swiper-slide', function(){
		var url = $(this).data('link');
		location.href = url;
		return;
	});

	function getLocation() {

		$.showPreloader('正在加载中...');
		var map = new BMap.Map("allmap");
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r){
			if(this.getStatus() == BMAP_STATUS_SUCCESS){

				getPositionInfo(r.point);
			} else {
				var myCity = new BMap.LocalCity();
				function myFun(result) {
					getPositionInfo(result.center);
				}
				myCity.get(myFun);
			}
		},{enableHighAccuracy: true})
	}

	function getPositionInfo(point) {
		$('#js-lat').html(point.lat);
		$('#js-lng').html(point.lng);
		var geoc = new BMap.Geocoder();
		geoc.getLocation(point, function(rs){
			var addComp = rs.addressComponents;
			var position =  addComp.city +  addComp.district +  addComp.street +  addComp.streetNumber;
			$('#position').html(position);
		});
		getStoreList();
		return ;
	}

	function getStoreList() {
		var params = {
			lat: $('#js-lat').html(),
			lng: $('#js-lng').html()
		}
		$.post("{php echo $this->createMobileUrl('index', array('op' => 'list'));}", params, function(data){
			var result = $.parseJSON(data);
			if(result.message.error != 0) {
				$.toast(result.message.message);
				return false;
			}
			if(result.message.message.length == 0) {
				$.hidePreloader();
				$('#store-list').addClass('store-empty');
				$('#store-list .common-no-con').removeClass('hide');
			} else {
				var gettpl = $('#tpl-store-list').html();
				laytpl(gettpl).render(result.message.message, function(html){
					$.hidePreloader();
					$('#store-list').removeClass('store-empty');
					$('#store-list .common-no-con').addClass('hide');
					$('#store-list').append(html);
					if(params.lng && params.lat) {
						var map = new BMap.Map("allmap");
						var pointA = new BMap.Point(params.lng, params.lat);
						$.each($('#store-list .no-dist'), function(){
							var lat = $(this).data('lat');
							var lng = $(this).data('lng');
							if(lat && lng) {
								var pointB = new BMap.Point(lng, lat);
								var dist = map.getDistance(pointA, pointB);
								$(this).find('.distance').attr('data-dist', dist/1000);
								if(dist > 1000) {
									dist = (dist / 1000).toFixed(2);
									$(this).find('.distance').removeClass('hide').html('<i class="fa fa-lbs"></i>' + dist + 'km');
								} else {
									dist = dist.toFixed(2);
									$(this).find('.distance').removeClass('hide').html('<i class="fa fa-lbs"></i>' + dist + 'm');
								}
							} else {
								$(this).find('.distance').attr('data-dist', 10000);
							}
						});
						var $applications = $('#store-list');
						var $data = $applications.clone();
						var $filteredData = $data.find('.no-dist');
						var $sortedData = $filteredData.sorted({
							by: function(v) {
								var $distance = $(v).find('.distance');
								return ($distance.attr('data-dist') * $distance.attr('data-in-business-hours'));
							}
						});
						$applications.quicksand($sortedData, {});
					}
				});
			}
		});
	}
	{if !$_GPC['d']}
		getLocation();
	{else}
		getStoreList();
	{/if}
});
</script>
{template 'common'}
{template 'footer'}